<?php


namespace app\queue\stomp;


use common\exception\LogicException;
use support\ErrorCode;
use think\module\Module;
use app\queue\BaseConsumer;
use common\model\VersionModel;
use common\model\ReviewRequestModel;
use common\model\ReviewRequestNodeModel;
use common\service\ReviewTaskService;

/**
 *  检查任务审核申请是否需要制片打包提交
 * Class CalculateEntityManHourConsumer
 * @package app\queue\redis
 */
class CheckReviewPlaylistStatusConsumer extends BaseConsumer
{
    // 要消费的队列名
    public $queue = 'check_review_playlist_status';

    // 连接名，对应 config/redis_queue.php 里的连接`
    public $connection = 'default';

    /**
     * @param $data
     * @return void
     * @throws \Throwable
     */
    public function doConsume($data)
    {
        try {
            $reviewRequestNode = model(ReviewRequestNodeModel::class)->find($data['id']);
            if (empty($reviewRequestNode)) {
                throw new LogicException('review request not found', ErrorCode::REVIEW_REQUEST_NOT_FOUND);
            }
            $reviewRequest = model(ReviewRequestModel::class)->find($reviewRequestNode['review_request_id']);
            if (empty($reviewRequest)) {
                throw new LogicException('review request not found', ErrorCode::REVIEW_REQUEST_NOT_FOUND);
            }
            // 版本审核
            if ($reviewRequest['module_id'] === Module::$moduleDictData['module_index_by_code']['version']['id']) {
                $version = model(VersionModel::class)->find($reviewRequest['link_id']);
                if (empty($version)) {
                    throw new LogicException("version not found [{$reviewRequest['link_id']}]", ErrorCode::VERSION_NOT_FOUND);
                }
                // 任务版本审核
                if ($version['module_id'] === Module::$moduleDictData['module_index_by_code']['task']['id']) {
                    trace('version review check playlist status', 'DEBUG');
                    ReviewTaskService::getInstance()->checkReviewNeedHandleStatus($reviewRequestNode['id'], $version);
                    ReviewTaskService::getInstance()->checkTaskRelatedPlaylistCanSubmit($reviewRequestNode['id']);
                }
            }
        } catch (\Throwable $e) {
            trace(format_exception_info($e), 'ERR');
        }
    }
}
